package starter.kit.rx.app.feature.book;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import starter.kit.rx.app.R;
import starter.kit.rx.app.model.bean.book.Books;
import starter.kit.rx.app.util.DisplayImgUtis;
import support.ui.adapters.EasyViewHolder;
import support.ui.utilities.ScreenUtils;

/**
 * ============================================================
 * 版权： xx 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/25 10:07.
 * 描述：
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */
public class BookViewHolder extends EasyViewHolder<Books> {


    @BindView(R.id.iV_film)
    ImageView mIVFilm;
    @BindView(R.id.tv_film_name)
    TextView mTvFilmName;
    @BindView(R.id.tv_film_grade)
    TextView mTvFilmGrade;
    @BindView(R.id.ll_book)
    LinearLayout mLlBook;

    private Context mContext;

    public BookViewHolder(Context context, ViewGroup parent) {
        super(context, parent, R.layout.item_book_reading);
        ButterKnife.bind(this, itemView);
        this.mContext = context;
    }

    @Override
    public void bindTo(int position, Books book) {
        ViewGroup.LayoutParams params = mIVFilm.getLayoutParams();
        int width = ScreenUtils.getScreenWidth();
        int ivWidth = (width - ScreenUtils.dp2px(80)) / 3;
        params.width = ivWidth;
        double height = (420.0 / 300.0) * ivWidth;
        params.height = (int) height;
        mIVFilm.setLayoutParams(params);
        if (!TextUtils.isEmpty(book.getImages().getLarge())) {
            DisplayImgUtis.getInstance().display(mContext, book.getImages().getLarge(), mIVFilm);
        }
        if (!TextUtils.isEmpty(book.getRating().getAverage())) {
            mTvFilmGrade.setText("评分:" + book.getRating().getAverage());
        } else {
            mTvFilmGrade.setText("暂无评分");
        }
        if (!TextUtils.isEmpty(book.getTitle())) {
            mTvFilmName.setText(book.getTitle());
        }
        mLlBook.setOnClickListener(view -> {
                Intent intent=new Intent(mContext, BookDetailActivity.class);
                intent.putExtra("id",book.getId());
                mContext.startActivity(intent);
        });
    }
}
